package com.qf.exam01.controller;
import com.qf.exam01.utils.IP;
import com.qf.exam01.utils.IPUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.*;

@RestController
@RequestMapping("/ip")
public class IPContoller {
    @Value("${qfsy.ip.file01}")
    String file01;
    @Value("${qfsy.ip.file02}")
    String file02;
    /**
     * 该接口生成指定数量的IP地址，并写入文本文件中
     * 文本文件的路径在yml文件中配置，key为 qfsy.ip.file01
     * @return
     */
    @GetMapping("/generate/{number}")
    public List<String> generateIP(@PathVariable("number") Integer number) throws IOException {
        BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file01));
        List<String> list = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            IP ip = IPUtils.randomIP(192, 168, 10);
            /*加入集合*/
            list.add(ip.toString());
            /*写入文件*/
            bufferedWriter.write(ip.toString());
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
        bufferedWriter.close();
        return list;
    }

    /**
     * 查询上次生成的随机IP地址
     * @return
     */
    @GetMapping("/query")
    public List<String> queryLast() throws IOException {
        List<String> list=getContentFromFile(file01);
        return list;
    }

    private List<String> getContentFromFile(String file01) throws IOException {
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file01));
        ArrayList<String> list = new ArrayList<>();
        String line=null;
        while ((line=bufferedReader.readLine())!=null){
            list.add(line);
        }
        bufferedReader.close();
        return  list;
    }


    /**
     * IP地址去重和统计
     * 1. 把不重复的IP地址写入一个新的文本文件中，文件配置在yml中，key为 qfsy.ip.file01
     * 2. 并在接口中返回
     * - 不重复的IP
     * - IP出现的次数
     * - 重复次数最多的5个IP
     * @return
     */
    @GetMapping("/s")
    public HashMap statistic() throws Exception {
        BufferedReader bufferedReader=new BufferedReader(new FileReader(file01));
        BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file02));
        ArrayList<String> list = new ArrayList<>();
        ArrayList<String> list2 = new ArrayList<>();
        HashMap<String, Integer> map = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        String line=null;
        while ((line=bufferedReader.readLine())!=null){
            list.add(line);
        }
        for (int i = 0; i < list.size(); i++) {
            if (!map.containsKey(list.get(i))){
                map.put(list.get(i),1);
            }else {
                map.put(list.get(i),map.get(list.get(i)).intValue()+1);
            }
        }
        Set<String> set = map.keySet();
        for (String key : set) {
           list2.add(key);
        }
        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
        Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                //正序排列，倒序反过来
                return o2.getValue() - o1.getValue();
            }
        });
        Map<String, Integer> result = new LinkedHashMap<>();
        int num=0;
        for (Map.Entry<String, Integer> entry : entryList) {
            if (num<5) {
                result.put(entry.getKey(), entry.getValue());
                num++;
            }else {
                break;
            }

        }
        hashMap.put("统计信息",map);
        hashMap.put("IP地址去重",list2);
        hashMap.put("重复次数最多的5个IP",result);
        bufferedWriter.write(map.toString());
        bufferedWriter.flush();
        bufferedWriter.close();
        bufferedReader.close();
        return hashMap;
    }


}
